lib: Split out helper function to create GPG context
authorColin Walters <walters@verbum.org>
Wed, 16 Nov 2016 14:10:39 +0000 (09:10 -0500)
committerAtomic Bot <atomic-devel@projectatomic.io>
Thu, 17 Nov 2016 09:44:07 +0000 (09:44 +0000)
In prep for future work.

Closes: #575
Approved by: giuseppe

src/libostree/ostree-repo.c
src/libotutil/ot-gpg-utils.c
src/libotutil/ot-gpg-utils.h

index 183feba5af6e0b053977f92cbbdc492a41be8638..584e570ce90f1cd0fb0b1ff6600f785c1f60ee40 100644 (file)
@@ -1479,13 +1479,9 @@ ostree_repo_remote_gpg_import (OstreeRepo         *self,
    * the keys to a new pubring.gpg file.  If the key data format is ASCII
    * armored, this step will convert them to binary. */
 
-  gpg_error = gpgme_new (&source_context);
-  if (gpg_error != GPG_ERR_NO_ERROR)
-    {
-      ot_gpgme_error_to_gio_error (gpg_error, error);
-      g_prefix_error (error, "Unable to create context: ");
-      goto out;
-    }
+  source_context = ot_gpgme_new_ctx (NULL, error);
+  if (!source_context)
+    goto out;
 
   if (source_stream != NULL)
     {
@@ -1569,13 +1565,9 @@ ostree_repo_remote_gpg_import (OstreeRepo         *self,
    * of the remote's keyring file.  We'll let the import operation alter
    * the pubring.gpg file, then rename it back to its permanent home. */
 
-  gpg_error = gpgme_new (&target_context);
-  if (gpg_error != GPG_ERR_NO_ERROR)
-    {
-      ot_gpgme_error_to_gio_error (gpg_error, error);
-      g_prefix_error (error, "Unable to create context: ");
-      goto out;
-    }
+  target_context = ot_gpgme_new_ctx (NULL, error);
+  if (!target_context)
+    goto out;
 
   /* No need for an output stream since we copy in a pubring.gpg. */
   if (!ot_gpgme_ctx_tmp_home_dir (target_context, tmp_dir, &target_tmp_dir,
@@ -3980,7 +3972,6 @@ sign_data (OstreeRepo     *self,
   g_autoptr(GOutputStream) tmp_signature_output = NULL;
   gpgme_ctx_t context = NULL;
   g_autoptr(GBytes) ret_signature = NULL;
-  gpgme_engine_info_t info;
   gpgme_error_t err;
   gpgme_key_t key = NULL;
   gpgme_data_t commit_buffer = NULL;
@@ -3992,34 +3983,9 @@ sign_data (OstreeRepo     *self,
     goto out;
   tmp_signature_output = g_unix_output_stream_new (tmp_fd, FALSE);
 
-  if ((err = gpgme_new (&context)) != GPG_ERR_NO_ERROR)
-    {
-      ot_gpgme_error_to_gio_error (err, error);
-      g_prefix_error (error, "Unable to create gpg context: ");
-      goto out;
-    }
-
-  info = gpgme_ctx_get_engine_info (context);
-
-  if ((err = gpgme_set_protocol (context, GPGME_PROTOCOL_OpenPGP)) !=
-      GPG_ERR_NO_ERROR)
-    {
-      ot_gpgme_error_to_gio_error (err, error);
-      g_prefix_error (error, "Unable to set gpg protocol: ");
-      goto out;
-    }
-  
-  if (homedir != NULL)
-    {
-      if ((err = gpgme_ctx_set_engine_info (context, info->protocol, NULL, homedir))
-          != GPG_ERR_NO_ERROR)
-        {
-          ot_gpgme_error_to_gio_error (err, error);
-          g_prefix_error (error, "Unable to set gpg homedir to '%s': ",
-                          homedir);
-          goto out;
-        }
-    }
+  context = ot_gpgme_new_ctx (homedir, error);
+  if (!context)
+    goto out;
 
   /* Get the secret keys with the given key id */
   err = gpgme_get_key (context, key_id, &key, 1);
@@ -4036,7 +4002,7 @@ sign_data (OstreeRepo     *self,
       g_prefix_error (error, "Unable to lookup key ID %s: ", key_id);
       goto out;
     }
-  
+
   /* Add the key to the context as a signer */
   if ((err = gpgme_signers_add (context, key)) != GPG_ERR_NO_ERROR)
     {
index 9042d88bd729507762d1d91f345368ad129d853f..b71f4845aa42f26f52a9450541676e933c6188be 100644 (file)
@@ -411,3 +411,36 @@ ot_gpgme_data_output (GOutputStream *output_stream)
 
   return data;
 }
+
+gpgme_ctx_t
+ot_gpgme_new_ctx (const char *homedir,
+                  GError    **error)
+{
+  gpgme_error_t err;
+  ot_auto_gpgme_ctx gpgme_ctx_t context = NULL;
+
+  if ((err = gpgme_new (&context)) != GPG_ERR_NO_ERROR)
+    {
+      ot_gpgme_error_to_gio_error (err, error);
+      g_prefix_error (error, "Unable to create gpg context: ");
+      return NULL;
+    }
+
+  if (homedir != NULL)
+    {
+      gpgme_engine_info_t info;
+
+      info = gpgme_ctx_get_engine_info (context);
+
+      if ((err = gpgme_ctx_set_engine_info (context, info->protocol, NULL, homedir))
+          != GPG_ERR_NO_ERROR)
+        {
+          ot_gpgme_error_to_gio_error (err, error);
+          g_prefix_error (error, "Unable to set gpg homedir to '%s': ",
+                          homedir);
+          return NULL;
+        }
+    }
+
+  return g_steal_pointer (&context);
+}
index b479a3a92a8dfedbfbd223f1f9ae85917223e6a6..c2337f7b6e191953f8952a9d09108715a519ab0e 100644 (file)
@@ -43,4 +43,7 @@ gboolean ot_gpgme_ctx_tmp_home_dir (gpgme_ctx_t     gpgme_ctx,
 gpgme_data_t ot_gpgme_data_input (GInputStream *input_stream);
 gpgme_data_t ot_gpgme_data_output (GOutputStream *output_stream);
 
+gpgme_ctx_t ot_gpgme_new_ctx (const char *homedir,
+                              GError    **error);
+
 G_END_DECLS